package com.insight.modules.message.handle.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import com.insight.common.util.SpringContextUtils;
import com.insight.common.util.oConvertUtils;
import com.insight.config.StaticConfig;
import com.insight.modules.message.handle.ISendMsgHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Service
@Slf4j
public class EmailSendMsgHandle implements ISendMsgHandle {
    static String emailFrom;

    public static void setEmailFrom(String emailFrom) {
        EmailSendMsgHandle.emailFrom = emailFrom;
    }
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void SendMsg(String es_receiver, String es_title, String es_content) {
        JavaMailSender mailSender = (JavaMailSender) SpringContextUtils.getBean("mailSender");
        MimeMessage message = mailSender.createMimeMessage();
        //update-begin-author：taoyan date:20200811 for:配置类数据获取
        if(oConvertUtils.isEmpty(emailFrom)){
            StaticConfig staticConfig = SpringContextUtils.getBean(StaticConfig.class);
            setEmailFrom(staticConfig.getEmailFrom());
        }
        //update-end-author：taoyan date:20200811 for:配置类数据获取
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            // 设置发送方邮箱地址
            helper.setFrom(emailFrom);
            helper.setTo(es_receiver);
            helper.setSubject(es_title);
            helper.setText(es_content, true);
            mailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
    @Override
    public boolean sendMailOnce(String es_receiver, String es_title, String es_content, byte[] bytes) {
        if (javaMailSender == null || oConvertUtils.isEmpty(es_receiver) || oConvertUtils.isEmpty(es_title) || es_content == null) {
            return false;
        }
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
            if (oConvertUtils.isEmpty(emailFrom)) {
                StaticConfig staticConfig = SpringContextUtils.getBean(StaticConfig.class);
                if (staticConfig != null) {
                    setEmailFrom(staticConfig.getEmailFrom());
                } else {
                    return false;
                }
            }
            messageHelper.setFrom(emailFrom);
            messageHelper.setTo(StringUtils.split(es_receiver, ","));
            messageHelper.setSubject(es_title);
            messageHelper.setText(es_content);
            if (ArrayUtils.isNotEmpty(bytes)) {
                messageHelper.addAttachment("电子保单.pdf", new ByteArrayResource(bytes), "application/pdf");
            }
            javaMailSender.send(mimeMessage);
            log.info("\n    [finished], 发送邮件成功");
            return true;
        } catch (Exception e) {
            log.error("\n    [error], 发送邮件过程中出现异常, e->{} ", e.getMessage(), e);
            return false;
        }
    }
}
